var resClass = require('restaurantClass')
var resLoader = require('../function/resLoader')
var UserData = require('UserData')
var restaruants = require('restaurants')
var COMMON_VALUE = require('Common')
cc.Class({
    extends: cc.Component,

    properties: {
        JPFOOD:{
            default:null,
            type:cc.Prefab,
        },
        BARBECUE:{
            default:null,
            type:cc.Prefab,
        },
        WESTERN:{
            default:null,
            type:cc.Prefab,
        },
        HOTPOT:{
            default:null,
            type:cc.Prefab,
        },
        HOMECOOKING:{
            default:null,
            type:cc.Prefab,
        },
        MILKYTEA:{
            default:null,
            type:cc.Prefab,
        },
        ICECREAM:{
            default:null,
            type:cc.Prefab,
        },
        FASTFOOD:{
            default:null,
            type:cc.Prefab,
        },
        SEAFOOD:{
            default:null,
            type:cc.Prefab,
        },
        CAKE:{
            default:null,
            type:cc.Prefab,
        },
        COFFEE:{
            default:null,
            type:cc.Prefab,
        },
        BAR:{
            default:null,
            type:cc.Prefab,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.buyList = COMMON_VALUE.RESTAURANT_BUY_MULTIPLY_BETWEEN_FLOOR_LIST
        this.getComponent()
        if(!(this.setupFinished ===1)){//防止ResSetup在start之前被调用的时候start重置active
            this.statusBar.active = false
        }
        this.UserData.runtimeState.buttonUpdateList.push([this.checkButtonStatus,this])
        this.UserData.updateButtonList()
        //基础价格
        let costMultiply = 1
        for(let i=1;i<=this.floor;i++){
            costMultiply = costMultiply * COMMON_VALUE.RESTAURANT_COST_MULTIPLY_BETWEEN_FLOOR_LIST[i-1]
        }
        this.upgradeManager.costMultiply =  costMultiply
        
        //基础收益
        let gainBaseNumber = 1 
        for(let i=1;i<=this.floor;i++){
            gainBaseNumber = gainBaseNumber * COMMON_VALUE.RESTAURANT_GAIN_MULTIPLY_BETWEEN_FLOOR_LIST[i-1]
        }
        this.upgradeManager.gainBaseNumber = 0
        this.upgradeManager.gainMultiply = gainBaseNumber
        this.upgradeManager.gainMultiplierList = COMMON_VALUE.RESTAURANT_GAIN_MULTIPLY_LIST.matrix[this.floor-1]
        
        if(this.floor !=1){//第一层免费
            this.button.getChildByName('Money Label').getComponent('labelFixed').setNumber(this.buyList[this.floor-1],"$")

        }else{
            this.button.getChildByName('Money Label').getComponent('labelFixed').string ="免费"
        }
        this.checkButtonStatus(this)
        this.resSetupStarted = true
        //console.log('ResSetup.start',this.floor)

    },
    getComponent(){
        this.lightList = []
        this.UserData = UserData
        this.leftCurtain = this.node.getChildByName("mask").getChildByName("左窗帘")
        this.rightCurtain = this.node.getChildByName("mask").getChildByName("右窗帘")
        this.upCurtain = this.node.getChildByName("mask").getChildByName("上窗帘")
        this.button = this.node.getChildByName('mask').getChildByName('Setup Button')
        this.statusBar = this.node.getChildByName('Status Bar')

        this.upgradeManager = this.node.getComponent('UpgradeManager')
        this.floor = this.node.getComponent('Restaurant').floor
    },
    checkButtonStatus(node){
        if(node.floor ===1){
            node.button.getComponent(cc.Button).interactable = true
        }else{
            if(node.UserData.getCurrentMoney()<node.buyList[node.floor-1]){
                node.button.getComponent(cc.Button).interactable = false
            }else{
                node.button.getComponent(cc.Button).interactable = true
            }
        }
        return node.button.getComponent(cc.Button).interactable
    },
    CurtainAnime (target){
        this.setupResTypeStr = target.currentTarget.name
        this.node.getChildByName('mask').getChildByName('选择页面').active =false
        let leftAnime = cc.moveTo(1,cc.v2(-this.node.width/2-200,this.leftCurtain.y)).easing(cc.easeBackIn())
        let rightAnime = cc.moveTo(1,cc.v2(this.node.width/2+200,this.rightCurtain.y)).easing(cc.easeBackIn())
        let upAnime = cc.moveTo(1,cc.v2(this.upCurtain.x,this.node.height)).easing(cc.easeBackIn())
        this.leftCurtain.runAction(leftAnime)
        this.rightCurtain.runAction(rightAnime)
        this.upCurtain.runAction(cc.sequence(upAnime,cc.callFunc(this.setupRes,this)))
        this.node.getChildByName('mask').getChildByName('Smoke').getComponent(cc.Animation).play()
    },
    resChoose(){
        if(!UserData.runtimeState.muteSound){
            cc.audioEngine.play(this.node.getComponent('Restaurant').setUpAudioClip)    
        }        
        try{
            if(this.UserData.getCurrentMoney()<this.buyList[this.floor-1] || this.floor !=1){//第一层免费，不扣钱
                this.UserData.subCurrentMoney(this.upgradeManager.costMultiply)
            }
            this.button.active =false
            this.node.getChildByName('mask').getChildByName('选择页面').active =true
        }catch(err){
            cc.log(err)
        }
    },
    setupRes(isLocal,resData) {
        let resComponent = this.node.getComponent('Restaurant')
        //console.log('ResSetup.setupRes',this.floor)
        this.leftCurtain.active = false
        this.rightCurtain.active = false
        this.upCurtain.active = false
        this.button.active =false
        let mask = this.node.getChildByName('mask')
        let resColor = null
        let res = null
        let resType = null
        this.setupResTypeStr = this.unifyKeyWord(this.setupResTypeStr)
        switch(this.setupResTypeStr){
            case "快餐":
                res = cc.instantiate(this.FASTFOOD)
                resColor = "yellow"
                resType = "FASTFOOD"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "快餐店"
            break;
            case "奶茶":
                res = cc.instantiate(this.MILKYTEA)
                resColor = "yellow"
                resType = "MILKYTEA"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "奶茶店"
            break;
            case "蛋糕":
                res = cc.instantiate(this.CAKE)
                resColor = "yellow"
                resType = "CAKE"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "蛋糕店"
            break;
            case "雪糕":
                res = cc.instantiate(this.ICECREAM)
                resColor = "yellow"
                resType = "ICECREAM"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "冰淇淋店"
            break;
            
            case "日料":
                res = cc.instantiate(this.JPFOOD)
                resColor = "blue"
                resType = "JPFOOD"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "日料店"
            break;
            case "酒吧":
                res = cc.instantiate(this.BAR)
                resColor = "blue"
                resType = "BAR"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "酒吧"
            break;
            case "烧烤":
                res = cc.instantiate(this.BARBECUE)
                resColor= "blue"
                resType = "BARBECUE"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "烧烤"
            break;
            case "咖啡":
                res = cc.instantiate(this.COFFEE)
                resColor = "blue"
                resType = "COFFEE"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "咖啡"
            break;
            
            case "西餐":
                res = cc.instantiate(this.WESTERN)
                resColor = "red"
                resType = "WESTERN"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "西餐"
            break;
            case "火锅":
                res = cc.instantiate(this.HOTPOT)
                resColor = "red"
                resType = "HOTPOT"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "火锅店"
            break;
            case "家常":
                res = cc.instantiate(this.HOMECOOKING)
                resColor = "red"
                resType = "HOMECOOKING"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "家常菜"
            break;
            case "海鲜":
                res = cc.instantiate(this.SEAFOOD)
                resColor = "red"
                resType = "SEAFOOD"
                this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string = "海鲜店"
            break;

            default:

        }
        this.showName = this.node.getChildByName('Status Bar').getChildByName('Transparent').getChildByName('label').getComponent(cc.Label).string

        if(res){
            this.node.getComponent('Restaurant').start()
            res.active =false

            mask.insertChild(res,1)
            this.node.getComponent('Gyroscope').resContentOriginalPos = cc.v2(res.x,res.y)
            this.node.getComponent('Gyroscope').desX = res.x
            this.node.getComponent('Gyroscope').desY = res.y

            this.findLight(this.node)

            this.node.getComponent('Restaurant').resSetup = true
            this.node.getComponent('Restaurant').chefButton.interactable = true
            this.node.getComponent('Restaurant').startWork("ResSetup")

            
            this.statusBar.active = true
            res.active = true
            
            if(this.UserData.runtimeState.day){
               this.lightOff() 
            }else{
               this.lightOn()
            }
            
            if(typeof(isLocal)==="object"){//是在本地创建时调用的
                this.node.getComponent('Restaurant').level = 1
                this.node.getChildByName('Status Bar').getChildByName('Level Pannel').getChildByName('Level Label').getComponent(cc.Label).string = "Lv.1"
                if(!res.getComponent("Flipin")){
                    res.addComponent("Flipin")
                }
                //更新远程数据
                restaruants.createRestaurant(0, this.floor, resType, resColor).then().catch(function (err) { //捕捉错误处理  
                    console.log(err);
                });

                //更新本地数据
                
                let i = this.floor-1
                UserData.state.restaurantList[i] = new resClass()
                UserData.state.restaurantList[i].floor = this.floor
                UserData.state.restaurantList[i].Level = 1                
                
                UserData.state.restaurantList[i].gainMultiplierList = COMMON_VALUE.RESTAURANT_GAIN_MULTIPLY_LIST[this.floor-1]

                //更新餐厅component内的数据
                this.node.getChildByName('Status Bar').getChildByName('Level Pannel').getChildByName('Level Label').getComponent(cc.Label).string = "Lv.1"                
                resComponent.node.getComponent('UpgradeManager').level = 1
                resComponent.level = 1
                resComponent.GainMultiplyList = UserData.state.restaurantList[i].gainMultiplierList

            }else{//从远程读取的餐厅数据
                
                this.node.getComponent('UpgradeManager').level = resData.Level
                this.node.getComponent('Restaurant').level = resData.Level
                this.node.getChildByName('Status Bar').getChildByName('Level Pannel').getChildByName('Level Label').getComponent(cc.Label).string = "Lv."+this.node.getComponent('Restaurant').level
                this.node.getComponent('UpgradeManager').costMultiply = resData.costMultiply
                this.node.getComponent('UpgradeManager').costBaseNumer = resData.costBaseNumer
                this.node.getComponent('UpgradeManager').gainBaseNumber = resData.gainBaseNumber
                this.node.getComponent('UpgradeManager').gainMultiply = resData.gainMultiply
                this.node.getComponent('UpgradeManager').gainMultiplierList  = COMMON_VALUE.RESTAURANT_GAIN_MULTIPLY_LIST.matrix[this.floor-1]
            }
            UserData.state.restaurantList[this.floor-1].restaurantType = resType
        }
        
        this.setupFinished = 1
        let edge = this.node.getChildByName("边框-AnimationCancel").getComponent(cc.Sprite)

        switch(resColor){
            case "red": resLoader.plistLoader("Restaurant/UI/Restaturant-1" , "餐厅外框 红"  ,edge)
            break
            case "yellow": resLoader.plistLoader("Restaurant/UI/Restaturant-1" , "餐厅外框 黄"  ,edge)
            break
            case "blue": resLoader.plistLoader("Restaurant/UI/Restaturant-1" , "餐厅外框 蓝"  ,edge)
            break
            default:resLoader.localImgLoader("Restaurant/UI/Restaturant-1/边框"  ,edge)
        }
        this.node.getComponent('Restaurant').resColor = resColor

        for(let i=0;i<this.upgradeManager.gainMultiplierList.length;i++){
            if( i% 6 === 5 & i != 0 ){
                resComponent.qualityUpgradeList.push(this.upgradeManager.gainMultiplierList[i][0])
            }
        }

        resComponent.updateResRuntimeData()
    },
    findLight(node){
        if(node.children.length>0){
            for(let i=0;i<node.children.length;i++){
                if(node.children[i].name.search("灯光") != -1){
                    this.lightList.push(node.children[i])
                }
                this.findLight(node.children[i])
            }
            /*if(node.getChildByName('灯光')){            
                this.lightList.push(node.getChildByName('灯光'))
            }*/
            /*for(var i=0;i<node.children.length;i++){
                this.findLight(node.children[i])
            }*/
        }else{
            return
        }        
    },
    unifyKeyWord(key){
        if(typeof(key)==="string"){
           key = key.toUpperCase() 
        }
        switch(key){
            case 1:
                return "日料"
            break;
            case "JPFOOD":
                return "日料"
            break;

            case 2:
                return "烧烤"
            break            
            case "BARBECUE":
                return "烧烤"
            break

            case 3:
                return "西餐"
            break            
            case "WESTERN":
                return "西餐"
            break

            case 4:
                return "火锅"
            break
            case "HOTPOT":
                return "火锅"
            break

            case 5:
                return "家常"
            break
            case "HOMECOOKING":
                return "家常"
            break

            case 6:
                return "奶茶"
            break
            case "MILKYTEA":
                return "奶茶"
            break

            case 7:
                return "雪糕"
            break
            case "ICECREAM":
                return "雪糕"
            break

            case 8:
                return "快餐"
            break
            case "FASTFOOD":
                return "快餐"
            break

            case 9:
                return "海鲜"
            break
            case "SEAFOOD":
                return "海鲜"
            break

            case 10:
                return "蛋糕"
            break
            case "CAKE":
                return "蛋糕"
            break

            case 11:
                return "咖啡"
            break
            case "COFFEE":
                return "咖啡"
            break
            
            case 12:
                return "酒吧"
            break
            case "BAR":
                return "酒吧"
            break
            
            default:
                return key
        }
    },
    
    lightOn (){
        this.node.getChildByName('mask').getChildByName('夜间变暗-AnimationCancel').active = true
        if(this.lightList){
            for(var i=0;i<this.lightList.length;i++){
                this.lightList[i].active = true
            }
        }
    },
    lightOff (){
        this.node.getChildByName('mask').getChildByName('夜间变暗-AnimationCancel').active = false
        if(this.lightList){
            for(var i=0;i<this.lightList.length;i++){
                this.lightList[i].active = false
            }
        }
    },


    // update (dt) {},
});
